package edu.ecu.seng6270.goa.ropa;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;
import javax.swing.SwingConstants;

/**
 * GUI Panel to allow user to create an Express order.
 */
public class ExpressOrderPanel extends JPanel {

	ROPAMain parent;
	
	private static final long serialVersionUID = -2335098498729452402L;

	private Order myOrder;
	private OrderItem myItem;
	private JLabel orderSubTotal;
	private JLabel moreDiscountsAlert;
	
	ExpressOrderPanel(final ROPAMain parent) {
		super();
		this.parent = parent;

		this.setBackground(new Color(255, 255, 255));
		this.setLayout(new MigLayout("", "[grow]", "[grow][grow][][]"));

		JLabel lblCustomOrder = new JLabel("Express Reprints");
		lblCustomOrder.setFont(new Font("Tahoma", Font.BOLD, 18));
		this.add(lblCustomOrder, "cell 0 0,alignx center");
		
		JPanel expressGrid = new JPanel();
		expressGrid.setBackground(new Color(255, 255, 255));
		add(expressGrid, "cell 0 1,alignx center,aligny center");
		expressGrid.setLayout(new MigLayout("", "[][grow]", "[][][][][][]"));
		
		JLabel lblPhotoSize = new JLabel("Photo Size:");
		expressGrid.add(lblPhotoSize, "cell 0 0,alignx trailing");
		
		final JComboBox photoSize = new JComboBox();
		photoSize.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myItem.setMySize(photoSize.getSelectedIndex()+1);
				updatePricingDisplay();
			}
		});
		photoSize.setModel(new DefaultComboBoxModel(new String[] {"4x6", "5x7", "8x10"}));
		expressGrid.add(photoSize, "cell 1 0,alignx leading");
		
		JLabel lblPhotoFinish = new JLabel("Photo Finish:");
		expressGrid.add(lblPhotoFinish, "cell 0 1,alignx trailing");
		
		final JComboBox photoFinish = new JComboBox();
		photoFinish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myItem.setMyFinish(photoFinish.getSelectedIndex()+1);
				updatePricingDisplay();
			}
		});
		photoFinish.setModel(new DefaultComboBoxModel(new String[] {"Glossy", "Matte"}));
		expressGrid.add(photoFinish, "cell 1 1,alignx leading");
		
		JLabel lblProcessingTime = new JLabel("Processing Time:");
		expressGrid.add(lblProcessingTime, "cell 0 2,alignx trailing");
		
		final JComboBox processingTime = new JComboBox();
		processingTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myItem.setMyPTime(processingTime.getSelectedIndex()+1);
				updatePricingDisplay();
			}
		});
		processingTime.setModel(new DefaultComboBoxModel(new String[] {"Next Day", "1 Hour"}));
		expressGrid.add(processingTime, "cell 1 2,alignx leading");
		
		JLabel lblQuantity = new JLabel("Quantity:");
		expressGrid.add(lblQuantity, "cell 0 3,alignx trailing");
		
		final JSpinner photoQuantity = new JSpinner();
		photoQuantity.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				myItem.setMyQuantity(Integer.valueOf(photoQuantity.getValue().toString()));
				updatePricingDisplay();
			}
		});
		photoQuantity.setModel(new SpinnerNumberModel(1, 1, 100, 1));
		final JFormattedTextField pqt = ((DefaultEditor) photoQuantity.getEditor()).getTextField();
		pqt.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent arg0) {
			}
			
			@Override
			public void keyReleased(KeyEvent arg0) {
				Integer spv = 0;
				try {
					spv = Integer.valueOf(pqt.getText());
				} catch (Exception e){
					spv = 1;
				}
				
				if (spv <= 1) {
					photoQuantity.setValue(1);
				}
				
				if (spv > 100) {
					photoQuantity.setValue(100);
				}
			}
			
			@Override
			public void keyPressed(KeyEvent arg0) {
			}

		});
		expressGrid.add(photoQuantity, "cell 1 3");
		
		JLabel photoID = new JLabel("Photo ID:");
		photoID.setHorizontalAlignment(SwingConstants.RIGHT);
		expressGrid.add(photoID, "cell 0 4,alignx trailing");
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Photo 1", "Photo 2", "Photo 3", "Photo 4", "Photo 5", "Photo 6", "Photo 7", "Photo 8", "Photo 9", "Photo 10"}));
		expressGrid.add(comboBox, "cell 1 4,growx");
		
		JLabel lblOrderSubtotal = new JLabel("Order Subtotal:");
		expressGrid.add(lblOrderSubtotal, "cell 0 5,alignx trailing");
		
		orderSubTotal = new JLabel("$0.00");
		expressGrid.add(orderSubTotal, "cell 1 5,alignx leading");
		
		JButton btnCancelOrder = new JButton("Cancel Order");
		btnCancelOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parent.cards.removeAll();
				parent.moveToCard("orderTypePanel");
			}
		});
		add(btnCancelOrder, "flowx,cell 0 2,alignx center");
		
		JButton btnProceedToCheckout = new JButton("Proceed to Checkout");
		btnProceedToCheckout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				parent.setPendingOrder(myOrder);
				parent.moveToCard("checkoutPanel");
			}
		});
		add(btnProceedToCheckout, "cell 0 2,alignx center");
		
		moreDiscountsAlert = new JLabel("This order may be eligible for ADDITIONAL DISCOUNTS during checkout! ");
		moreDiscountsAlert.setForeground(new Color(0, 102, 0));
		moreDiscountsAlert.setFont(new Font("Tahoma", Font.BOLD, 10));
		add(moreDiscountsAlert, "cell 0 3,alignx center");
		moreDiscountsAlert.setVisible(false);
		
		//////////////////////////////////////////
		// 		Create an Order and OrderItem	//
		//////////////////////////////////////////
		
		myOrder = new Order(parent.getMyCustomerID());
		myOrder.setIsExpress(true);
		myItem = new OrderItem();
		
		if (! myOrder.insertOrderItem(myItem)) {
			System.out.println("Error inserting order item into order.");
		}

		updatePricingDisplay();

	}


	/**
	 * Convenience method that causes the order to
	 * recalculate and updates the ExpressOrder display
	 */
	public void updatePricingDisplay () {
		myOrder.calculateOrderPrice();
		orderSubTotal.setText(String.format("$%.2f", myOrder.getNonDiscountedPrice() ));
		if (myOrder.getNonDiscountedPrice() > 35.0 || myOrder.getPromoEligible()) { 
			moreDiscountsAlert.setVisible(true); 
		} else {
			moreDiscountsAlert.setVisible(false);
		}
	}
	
	
}
